$(function () {
    syb("#signOrgName");
    $("#signOrgName option").remove();
    $("#signOrgName").prepend("<option value='0'>全部</option>");
    loadCertificateType('190px', '#certificateType', $("#GENERAL_CREDENTIAL").val(), 0);

    $("#certificateSpecialtys").select2({
        placeholder: "请选择",
        allowClear: true,
        width: "190px",
        language: "zh-CN"
    });

    $('#myTable').dataTableExtend({
        "ajax": sysInfo.basePath + "/zs/unused/warning/query_unused_warning.do",
//			    	"select":true,
        "scrollX": "true",//是否开启横项滚动
        "fixedColumns": {//锁定列，只有开启scrollX时才生效
            "leftColumns": 2,//左侧锁定2列
            "rightColumns": 1//右侧锁定1列
        },
        //搜索条件
        "fnServerParams": function (aoData) {
            var signOrgId = $('#signOrgName').val();
            if (signOrgId == 0) {
                signOrgId = "";
            }
            aoData.signOrgId = signOrgId;
            var checkResult = checkHasSybAuth($('#signOrgName option'));
            if (!checkResult) {
                aoData.isAll = "0";
            }
            if ($('#certificateType').val()) {
                aoData.certificateType = $("#certificateType").val();
            }
            if ($('#certificateSpecialtys').val()) {
                aoData.certificateSpecialtys = $("#certificateSpecialtys").val();
            }
            if ($('#certificateLevel').val()) {
                aoData.certificateLevel = $("#certificateLevel").val();
            }
            if ($('#personalPhone').val()) {
                aoData.personalPhone = $('#personalPhone').val();
            }
            if ($('#personalIdCard').val()) {
                aoData.personalIdCard = $('#personalIdCard').val();
            }
            if ($('#keyword').val()) {
                aoData.keyword = $('#keyword').val();
            }
        },
        "aoColumns": [
            {
                "title": "所属事业部",
                "data": "signOrgName",
                "orderable": false
            },
            {
                "title": "关联生产订单",
                "data": "scOrderNo",
                "orderable": false,
                "render": function (data, type, row, meta) {
                    if (!data) {
                        return "-";
                    }
                    return "<a href='javascript:void(0);'  class='layui-table-link' onclick='openOrderInfoDetailFromIframe(\"" + data + "\",\"" + row.scOrderId + "\")'>" + data + "</a>";
                }
            },
            {
                "title": "商机编号",
                "data": "businessNo",
                "orderable": false,
                "render": function (data, type, row, meta) {
                    if (!data) {
                        return "";
                    }
                    return data;
                }
            },
            {
                "title": "收证商务",
                "data": "collectorName",
                "orderable": false,
                "render": function (data, type, full, meta) {
                    return "<span>" + full.collectorName + "</span>";
                }
            },
            {
                "title": "证书编号",
                "data": "certificateNo",
                "orderable": false,
                "render": function (data, type, full, meta) {
                    return full.certificateName + "<br/><a href='javascript:void(0);'  class='layui-table-link' onclick='credentialDetail(\"" + data + "\",\"" + full.id + "\")'>" + data + "</a>";
                }
            },
            {
                "title": "持证人/联系方式",
                "data": "personalIdCard",
                "orderable": false,
                "render": function (data, type, full, meta) {
                    return full.personalName + "<br>" + full.personalPhone;
                }
            },
            {
                "title": "身份证号码",
                "data": "personalIdCard",
                "orderable": false,
                "render": function (data, type, full, meta) {
                    return checkIsEmpty(data) ? "-" : "<span>" + hideIdCard(data) + "</span>";
                }
            },
            {
                "title": "类型/专业/等级",
                "data": "certificateSpecialtys",
                "orderable": false,
                "render": function (data, type, row, meta) {
                    data = (null != data && "" != data) ? data : "";
                    return row.certificateType + "<div title='" + data + "' class='f-toe' style=\"max-width:100px;word-wrap:break-word\">" + data + "</div>" + row.certificateLevel;
                }
            },
            {
                "title": "最新入库时间",
                "data": "lastWarehousingTime",
                "orderable": false,
                "width": "190"
            },
            {
                "title": "未使用起始时间",
                "data": "lastUserTime",
                "orderable": false,
                "render": function (data) {
                    if (!data) {
                        return "";
                    }
                    return data.substring(0, 10);
                }
            },
            {
                "title": "证书未使用预警提醒",
                "data": "lastUserTime",
                "orderable": false,
                "render": function (data, type, full, meta) {
                    if (!data) {
                        return "";
                    }
                    var day = parseInt((Date.parse(new Date()) - Date.parse(data)) / (1000 * 3600 * 24));
                    return "<span style='color:#FF0000;'>已" + day + "天未使用</span>";
                }
            },
            {
                "title": "操作",
                "data": "id",
                "orderable": false,
                'sClass': "text-center",
                "render": function (data, type, full, meta) {
                    var td = '';
                    td += '<a class="iboss-link" href="javascript:void(0)" onclick="editCertificate(\'' + data + '\')">&ensp;&ensp;编辑</a><br>';
                    td += '<a class="iboss-link" href="javascript:void(0)" onclick="affiliatedCredential(\'' + data + '\')">使用证书</a>';
                    return td;
                }
            }
        ]
    });
});


function earlyWarningTime() {
    var index = dgg.Open({
        title: '预警时间设置',
        content: $('#earlyWarningTime'),
        area: '300px',
        yes: function () {
            var warningValue = $("#warningValue").val();
            if (!warningValue) {
                DGG.Warning('请输入预警值');
                return false;
            }
            var signOrgId = $("#signOrgName").val();
            if (0 == signOrgId) {
                signOrgId = "";
            }
            DGG.loading();
            $.ajax({
                type: "post",
                url: sysInfo.basePath + "/zs/warning_configure/update_single_warning.do",
                data: {
                    "warningName": "CERTIFICATE_NOT_USED_WARNING",
                    "signOrgId": signOrgId,
                    "warningValue": warningValue,
                    "warningType": $("#GENERAL_CREDENTIAL").val()
                },
                dataType: "json",
                success: function (result) {
                    DGG.loading('close');
                    if (result.code == 0) {
                        layer.close(index);
                        $("#expireWarning").html(warningValue);
                        DGG.Succeed('修改成功');
                        $('#myTable').dataTableExtend("reload");
                    } else {
                        DGG.Errors(result.msg);
                    }
                }
            });
        }

    })
}

/**
 * 数据导出
 */
function exportExcel() {
    var aoData = {};
    var signOrgId = $('#signOrgName').val();
    if (signOrgId == 0) {
        signOrgId = "";
    }
    aoData.signOrgId = signOrgId;
    var checkResult = checkHasSybAuth($('#signOrgName option'));
    if (!checkResult) {
        aoData.isAll = "0";
    }
    if ($('#certificateType').val()) {
        aoData.certificateType = $("#certificateType").val();
    }
    if ($('#certificateSpecialtys').val()) {
        aoData.certificateSpecialtys = $("#certificateSpecialtys").val();
    }
    if ($('#certificateLevel').val()) {
        aoData.certificateLevel = $("#certificateLevel").val();
    }
    if ($('#personalPhone').val()) {
        aoData.personalPhone = $('#personalPhone').val();
    }
    if ($('#personalIdCard').val()) {
        aoData.personalIdCard = $('#personalIdCard').val();
    }
    if ($('#keyword').val()) {
        aoData.keyword = $('#keyword').val();
    }
    export_table("/zs/unused/warning/export.do", aoData);
}

/**
 * 使用证书
 */
function affiliatedCredential(id) {
    var certificateId = id;
    DGG.loading();
    $.post(sysInfo.basePath + "/zs/general_credential/check_credential_status.do", {certificateId: certificateId}, function (result) {
        if (result.code == 0) {
            $.post(sysInfo.basePath + "/zs/general_credential/affiliated_certificate_page.html", {certificateId: certificateId}, function (data) {
                dgg.Open({
                    title: '使用证书',
                    content: data,
                    area: '500px',
                    btn: ["确定", "取消"],
                    yes: function () {
                        DGG.loading('close');
                        confirmAffiliated(certificateId)
                    }
                });
            }, 'html');
        } else {
            DGG.Errors(result.msg);
            DGG.loading('close');
        }
    });
}
